package com.wenan.ui.controls.listBoxes
{
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	import mx.controls.Label;
	import mx.controls.RadioButton;
	import mx.controls.RadioButtonGroup;
	
	public class RadioButtomItemRenderer extends RadioButton
	{
		private var isRegisted:Boolean = false;
		public var checkField:String = "selected";
		private var ml:Label = null;
		
		public function RadioButtomItemRenderer()
		{
			var radioGroup:RadioButtonGroup = new RadioButtonGroup();
			super();
			this.setStyle("paddingLeft", 5);
			this.setStyle("paddingRight", 5);
			this.setStyle("fontStyle", "italic");
			this.setStyle("fontSize", 13);
			this.group = radioGroup;
			this.addEventListener(MouseEvent.CLICK, mouseClickHandler);
		}
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			this.label = value.displayName;
			if (!isRegisted) {
				isRegisted = true;
				var dg:DataGrid = DataGrid(listData.owner);  
				var cc:CheckBoxColumn=dg.columns[listData.columnIndex];  
				this.checkField=cc.checkField;  
			}  
			this.selected=value[this.checkField];  
		}  
		
		private function mouseClickHandler(e:MouseEvent):void
		{
			var dg:DataGrid = DataGrid(listData.owner)  
			var cc:CheckBoxColumn=dg.columns[listData.columnIndex];
			notSelectAll();
			dg.dispatchEvent(new CheckListBoxItemEvent(CheckListBoxItemEvent.ITEM_SELECTED, data));
			cc.header.selected=false;
			this.data[this.checkField]=this.selected;  
			
		}  

		private function isAll():Boolean
		{ 
			var dg:DataGrid = DataGrid(listData.owner)  
			var s:ArrayCollection = ArrayCollection(dg.dataProvider);  
			for(var i:int=0;i<s.length;i++)
			{ 
				if(!s[i][this.checkField])
				{  
					return false  
				}                 
			}         
			return true  
		}
		
		private function notSelectAll():void
		{ 
			var dg:DataGrid = DataGrid(listData.owner)  
			var s:ArrayCollection = ArrayCollection(dg.dataProvider);  
			for(var i:int=0;i<s.length;i++)
			{ 
				if(s[i][this.checkField])
				{  
					s[i][this.checkField] = false;
				}                 
			}         
			dg.invalidateList();
		}
	}
}